package com.catsic.zcjc.util;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * String 工具类
 * @author sonta
 *
 */
public class StringUtil {
	
	public static final String EMPTY = "";
	public final static String HTML_LINE_SEPERATOR = "<br/>";
	
	// 随机字符集
	private static final char[] chars = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
		'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
		'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	// 随机数字字符集
	private static final char[] numberChars = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	
	/**
	 * 字符串左边补零
	 * @param oriStr 原始字符串
	 * @param length 字符串总长度
	 * @return
	 */
	public static String leftZero (String oriStr, int length) {
		if (oriStr == null) {
			return null;
		}
		StringBuffer temp = new StringBuffer();
		int zeroLength = length - oriStr.length();
		for (int i = 0; i < zeroLength; i++) {
			temp.append("0");
		}
		temp.append(oriStr);
		return temp.toString();
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty (String str) {
		return str == null || "".equals(str.trim());
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty (String str) {
		return str != null && !"".equals(str.trim());
	}
	
	/**
	 * null 转 ''
	 * @param str
	 * @return
	 */
	public static String null2Empty(String str) {
		return str == null ? "" : str.trim();
	}
	
	/**
	 * 将字符串根据指定分隔符转为list
	 * @param str
	 * @param seperater
	 * @return
	 */
	public static List<String> getList(String str, String seperater) {
		List<String> list = new ArrayList<String>();
		String[] arr = str.split(seperater);
		for (String s : arr) {
			list.add(s);
		}
		return list;
	}
	
	/**
	 * 判断两个字符串是否相等  如果都为 null 返回 true
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean equal(String str1, String str2) {
		return str1 == null ? str2 == null : str1.equals(str2);
	}
	
	/**
	 * 生成指定长度的随机数字字符串
	 * @param length
	 * @return
	 */
	public static String getRandomNumber(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(numberChars[new Random().nextInt(numberChars.length)]);
		}
		return sb.toString();
	}
	
	/**
	 * 生成指定长度的随机字母数字字符串
	 * @param length
	 * @return
	 */
	public static String getRandomStr(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(chars[new Random().nextInt(chars.length)]);
		}
		return sb.toString();
	}
	
	public static String getCategoryCode(int index,int length){
		String s = index+"";
		int strLength = s.length();
		int chai = length-strLength;
		
		for(int i=0;i<chai;i++){
			s = "0"+s;
		}
		
		return s;
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 100; i++){
			System.out.println(StringUtil.getRandomStr(6));
		}
	}
	
	public static String filterOffUtf8Mb4_2(String text) throws Exception{
		byte[] bytes = text.getBytes("utf-8");
		ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
		int i = 0;
		while (i < bytes.length) {
		short b = bytes[i];
		if (b > 0) {
		buffer.put(bytes[i++]);
		continue;
		}

		b += 256; //去掉符号位

		if (((b >> 5) ^ 0x06) == 0) {
		buffer.put(bytes, i, 2);
		i += 2;
		System.out.println("2");
		} else if (((b >> 4) ^ 0x0E) == 0) {
		System.out.println("3");
		buffer.put(bytes, i, 3);
		i += 3;
		} else if (((b >> 3) ^ 0x1E) == 0) {
		i += 4;
		System.out.println("4");
		} else if (((b >> 2) ^ 0xBE) == 0) {
		i += 5;
		System.out.println("5");
		} else {
		i += 6;
		System.out.println("6");
		}
		}
		buffer.flip();
		return new String(buffer.array(), "utf-8");
	}

	public static boolean isEnglish(String charaString) {
		return charaString.matches("^[a-zA-Z]*");
	}
	
	public static String getMapStr(Map<String,Object> map,String key){
		Object o = map.get(key);
		
		if(o!=null){
			return o.toString();
		}
		
		return null;
	}
	
	public static int getMapInt(Map<String,Object> map,String key){
		Object o = map.get(key);
		
		if(o!=null){
			return Integer.parseInt(o.toString());
		}
		
		return 0;
	}
	
	public static Double getMapDouble(Map<String,Object> map,String key){
		Object o = map.get(key);
		
		if(o!=null){
			return Double.parseDouble(o.toString());
		}
		
		return null;
	}
	
	public static Double getMapDouble(Map<String,Object> map,String key,int scale){
		Object o = map.get(key);
		
		if(o!=null){
			BigDecimal m = new BigDecimal(o.toString());
			return  m.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();  
		}
		
		return null;
	}
}
